# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from DjangoUeditor.forms import UEditorField
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render
from requests import Response
from django.forms import forms

from utils.responseutils import response_change


def config(requests):
    upfile = requests.FILES.get("upfile")

    config_dict = {
        "videoMaxSize": 102400000,
        "videoActionName": "uploadvideo",
        "fileActionName": "uploadfile",
        "fileManagerListPath": "/ueditor/upload/file/",
        "imageCompressBorder": 1600,
        "imageManagerAllowFiles": [
            ".png",
            ".jpg",
            ".jpeg",
            ".gif",
            ".bmp"
        ],
        "imageManagerListPath": "/ueditor/upload/image/",
        "fileMaxSize": 51200000,
        "fileManagerAllowFiles": [
            ".png",
            ".jpg",
            ".jpeg",
            ".gif",
            ".bmp",
            ".flv",
            ".swf",
            ".mkv",
            ".avi",
            ".rm",
            ".rmvb",
            ".mpeg",
            ".mpg",
            ".ogg",
            ".ogv",
            ".mov",
            ".wmv",
            ".mp4",
            ".webm",
            ".mp3",
            ".wav",
            ".mid",
            ".rar",
            ".zip",
            ".tar",
            ".gz",
            ".7z",
            ".bz2",
            ".cab",
            ".iso",
            ".doc",
            ".docx",
            ".xls",
            ".xlsx",
            ".ppt",
            ".pptx",
            ".pdf",
            ".txt",
            ".md",
            ".xml"
        ],
        "fileManagerActionName": "listfile",
        "snapscreenInsertAlign": "none",
        "scrawlActionName": "uploadscrawl",
        "videoFieldName": "upfile",
        "imageCompressEnable": True,
        "videoUrlPrefix": "",
        "fileManagerUrlPrefix": "",
        "catcherAllowFiles": [
            ".png",
            ".jpg",
            ".jpeg",
            ".gif",
            ".bmp"
        ],
        "imageManagerActionName": "listimage",
        "snapscreenPathFormat": "/ueditor/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}",
        "scrawlPathFormat": "/ueditor/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}",
        "scrawlMaxSize": 2048000,
        "imageInsertAlign": "none",
        "catcherPathFormat": "/ueditor/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}",
        "catcherMaxSize": 2048000,
        "snapscreenUrlPrefix": "",
        "imagePathFormat": "/ueditor/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}",
        "imageManagerUrlPrefix": "",
        "scrawlUrlPrefix": "",
        "scrawlFieldName": "upfile",
        "imageMaxSize": 2048000,
        "imageAllowFiles": [
            ".png",
            ".jpg",
            ".jpeg",
            ".gif",
            ".bmp"
        ],
        "snapscreenActionName": "uploadimage",
        "catcherActionName": "catchimage",
        "fileFieldName": "upfile",
        "fileUrlPrefix": "",
        "imageManagerInsertAlign": "none",
        "catcherLocalDomain": [
            "127.0.0.1",
            "localhost",
            "img.baidu.com"
        ],
        "filePathFormat": "/ueditor/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}",
        "videoPathFormat": "/ueditor/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}",
        "fileManagerListSize": 20,
        "imageActionName": "uploadimage",
        "imageFieldName": "upfile",
        "imageUrlPrefix": "",
        "scrawlInsertAlign": "none",
        "fileAllowFiles": [
            ".png",
            ".jpg",
            ".jpeg",
            ".gif",
            ".bmp",
            ".flv",
            ".swf",
            ".mkv",
            ".avi",
            ".rm",
            ".rmvb",
            ".mpeg",
            ".mpg",
            ".ogg",
            ".ogv",
            ".mov",
            ".wmv",
            ".mp4",
            ".webm",
            ".mp3",
            ".wav",
            ".mid",
            ".rar",
            ".zip",
            ".tar",
            ".gz",
            ".7z",
            ".bz2",
            ".cab",
            ".iso",
            ".doc",
            ".docx",
            ".xls",
            ".xlsx",
            ".ppt",
            ".pptx",
            ".pdf",
            ".txt",
            ".md",
            ".xml"
        ],
        "catcherUrlPrefix": "",
        "imageManagerListSize": 20,
        "catcherFieldName": "source",
        "videoAllowFiles": [
            ".flv",
            ".swf",
            ".mkv",
            ".avi",
            ".rm",
            ".rmvb",
            ".mpeg",
            ".mpg",
            ".ogg",
            ".ogv",
            ".mov",
            ".wmv",
            ".mp4",
            ".webm",
            ".mp3",
            ".wav",
            ".mid"
        ]
    }
    return response_change(JsonResponse(config_dict))


def uploadimage(requests):
    upfile = requests.FILES.get("upfile")
    upfile_resp = {"state": "SUCCESS", "original": "450721153367335910.jpg", "size": "37253",
                   "title": "1545891123512037028.jpg",
                   "type": ".jpg", "url": "/ueditor/jsp/upload/image/20181227/1545891123512037028.jpg"}
    return JsonResponse(upfile_resp)


def uploadscrawl(requests):
    upfile = requests.FILES.get("content")
    resp = {
        "state": "SUCCESS",
        "url": "upload/demo.jpg",
        "title": "demo.jpg",
        "original": "demo.jpg"
    }
    return JsonResponse(resp)


def uploadvideo(requests):
    upfile = requests.FILES.get("upfile")
    resp = {
        "state": "SUCCESS",
        "url": "upload/demo.mp4",
        "title": "demo.mp4",
        "original": "demo.mp4"
    }
    return JsonResponse(resp)


def uploadfile(requests):
    upfile = requests.FILES.get("upfile")
    resp = {
        "state": "SUCCESS",
        "url": "upload/demo.zip",
        "title": "demo.zip",
        "original": "demo.zip"
    }
    return JsonResponse(resp)


def listimage(requests):
    resp = {
        "state": "SUCCESS",
        "list": [{
            "url": "upload/1.jpg"
        }, {
            "url": "upload/2.jpg"
        }, ],
        "start": 20,
        "total": 100
    }
    return JsonResponse(resp)


def catchimage(requests):
    resp = {
        "state": "SUCCESS",
        "list": [{
            "url": "upload/1.jpg",
            "source": "http://b.com/2.jpg",
            "state": "SUCCESS"
        }, {
            "url": "upload/2.jpg",
            "source": "http://b.com/2.jpg",
            "state": "SUCCESS"
        }, ]
    }
    return JsonResponse(resp)


def controller(requests):
    action = requests.GET.get("action")
    if action == "config":
        config(requests)
    elif action == "uploadimage":
        uploadimage(requests)
    elif action == "uploadscrawl":
        uploadscrawl(requests)
    elif action == "uploadvideo":
        uploadvideo(requests)
    elif action == "uploadfile":
        uploadfile(requests)
    elif action == "listimage":
        listimage(requests)
    elif action == "catchimage":
        catchimage(requests)


class TestUEditorForm(forms.Form):
    content = UEditorField('内容', width=600, height=300, toolbars="full", imagePath="static/images/", filePath="static/files/",
                           upload_settings={"imageMaxSize": 1204000}, settings={})


def index(requests):
    form = TestUEditorForm()
    return render(requests, "index.html", {"form": form})
